package com.king.photo.activity;

import java.io.File;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.king.photo.R;
import com.king.photo.util.BitmapUtil;
import com.king.photo.util.FileUtils;

public class SaveActivity extends Activity implements OnClickListener {

	private ImageView mImageView;
	private Button back,cancel;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.save_layout);
		initView();
	}

	int mCircleCenterX ;
	int mCircleCenterY ;

	private void initView() {
		mImageView = (ImageView) findViewById(R.id.image);
		mImageView.setOnClickListener(this);
		back = (Button)findViewById(R.id.back);
		cancel = (Button)findViewById(R.id.cancel);
		back.setText("返回");
		cancel.setText("保存");
		back.setOnClickListener(this);
		cancel.setOnClickListener(this);
		if (MainActivity.bitmap == null) {
			return;
		}
		mCircleCenterX = mImageView.getWidth() / 2;
		mCircleCenterY = mImageView.getHeight() / 2;
		// 裁剪为圆形图片
		 DisplayMetrics dm = new DisplayMetrics();
		 getWindowManager().getDefaultDisplay().getMetrics(dm);
		 int screenWidth = dm.widthPixels;
		 int screenHeigh = dm.heightPixels -100;
		
		Bitmap bitmap = BitmapUtil.getInstance().toRoundBitmap(
				MainActivity.bitmap, this,screenWidth/2,screenHeigh/2 );
		mImageView.setImageBitmap(bitmap);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.image:

			break;
		case R.id.back:
			finish();
			break;
		case R.id.cancel:
			mImageView.setDrawingCacheEnabled(true);
			Bitmap bitmap = mImageView.getDrawingCache().copy(mImageView.getDrawingCache().getConfig(),
					false);
			mImageView.setDrawingCacheEnabled(false);
			 String SDPATH = Environment.getExternalStorageDirectory()
					+ "/crop/heart";
			String picName = System.currentTimeMillis()+"";
			if(BitmapUtil.saveBitmapToSDCard(bitmap, SDPATH, picName)){
				Toast.makeText(getApplicationContext(), "保存成功 保存到："+SDPATH+ picName, Toast.LENGTH_LONG).show();
			}else {
				Toast.makeText(getApplicationContext(), "保存失败", Toast.LENGTH_LONG).show();
			}
			finish();
			break;
		default:
			break;
		}
	}
}
